﻿using System;
using System.Web;
using System.Configuration;

namespace EFB.Lib {

    public class ContextHelper {

        public static Object Item(String Name) {
            return HttpContext.Current.Session[Name];
        }

        public static void WriteItem(String Name, Object Value) {
            RemoveItem(Name);
            HttpContext.Current.Session.Add(Name, Value);
        }

        public static void RemoveItem(String Name) {
            HttpContext.Current.Session.Remove(Name);
        }

        public static String ConfigItem(String Name){
            return ConfigurationManager.AppSettings[Name];
        }

        public static String ConnectionStringItem(String Name) {
            return ConfigurationManager.ConnectionStrings[Name].ConnectionString;
        }

        public static String FormatExceptionText(Exception Except) {
            String Result = "Mensagem: " + Except.Message;
            if (ConfigItem("PrintStackTrace").Equals("true", StringComparison.InvariantCultureIgnoreCase)) {
                Result += Environment.NewLine + Environment.NewLine + "StackTrace: " + Except.StackTrace;
            }
            return Result.Replace(Environment.NewLine, @"<br />").Replace(@"\r\n", @"<br />");
        }

    }

}

